package zeroj.web.interceptors;

import java.io.IOException;

import zeroj.web.action.ActionHelper;
import zeroj.web.context.ValueStack;
import zeroj.web.dispatch.ActionMapping;
import zeroj.web.dispatch.Dispatcher;

/**
 * 
 * 
 * @author lyl(lylsir at gmail dot com)
 * 
 * @since 1.0
 * 
 * 2008-9-10
 */
public class LoginRequiredInterceptor extends InterceptorAdaptor {

	@Override
	public String preExcute(final ActionMapping mapping) {

		ValueStack stack = Dispatcher.getRequest().getValueStack();
		String role = mapping.getInfo().getLoginRole();
		if(! stack.isLogin() ||(role != null && ! role.equals(stack.getLoginRole()))) {
			String loginUri = mapping.getInfo().getLoginUri();
			loginUri = loginUri == null ? "account/login" : loginUri;
			String url = ActionHelper.getUrl(loginUri);
			try {
				Dispatcher.getResponse().sendRedirect(url);
			} catch (IOException e) {
			}
		}
		return super.preExcute(mapping);
	}
}
